import data_storage from '@ohos.data.storage';
import Config from './config.js';

export default {
    getInstance() {
        const PATH = '/data/data/com.acedemo.game2048/game2048';
        let store = data_storage.getStorageSync(PATH);
        return store;
    },
/**
     * 保存最高分
     * @param bestScore
     */
    putBestScore(bestScore) {
        let store = this.getInstance();
        switch (Config.GRIDColumnCount) {
            case 4: // 难度4
            {
                store.putSync(Config.KEY_BEST_SCORE_WITHIN_4, bestScore);
                break;
            }
            case 5: // 难度5
            {
                store.putSync(Config.KEY_BEST_SCORE_WITHIN_5, bestScore);
                break;
            }
            case 6: // 难度6
            {
                store.putSync(Config.KEY_BEST_SCORE_WITHIN_6, bestScore);
                break;
            }
            default:
                break;
        }
    },
/**
     * 获取最高分
     * @return bestScore
     */
    getBestScore() {
        let store = this.getInstance();
        let bestScore = 0;
        switch (Config.GRIDColumnCount) {
            case 4: // 难度4
            {
                bestScore = store.getSync(Config.KEY_BEST_SCORE_WITHIN_4, 0);
                break;
            }
            case 5: // 难度5
            {
                bestScore = store.getSync(Config.KEY_BEST_SCORE_WITHIN_5, 0);
                break;
            }
            case 6: // 难度6
            {
                bestScore = store.getSync(Config.KEY_BEST_SCORE_WITHIN_6, 0);
                break;
            }
            default:
                break;
        }
        return bestScore;
    },
/**
     * 保存当前得分
     * @param currentScore
     */
    putCurrentScore(currentScore) {
        let store = this.getInstance();
        switch (Config.GRIDColumnCount) {
            case 4: // 难度4
            {
                store.putSync("KEY_CURRENT_SCORE_4", currentScore);
                break;
            }
            case 5: // 难度5
            {
                store.putSync("KEY_CURRENT_SCORE_5", currentScore);
                break;
            }
            case 6: // 难度6
            {
                store.putSync("KEY_CURRENT_SCORE_6", currentScore);
                break;
            }
            default:
                break;
        }
    },
/**
     * 获取最高分
     * @return bestScore
     */
    getCurrentScore() {
        let store = this.getInstance();
        let currentScore = 0;
        switch (Config.GRIDColumnCount) {
            case 4: // 难度4
            {
                currentScore = store.getSync("KEY_CURRENT_SCORE_4", 0);
                break;
            }
            case 5: // 难度5
            {
                currentScore = store.getSync("KEY_CURRENT_SCORE_5", 0);
                break;
            }
            case 6: // 难度6
            {
                currentScore = store.getSync("KEY_CURRENT_SCORE_6", 0);
                break;
            }
            default:
                break;
        }
        return currentScore;
    },
/**
     * 保存无限模式下最高分
     * @param bestScore
     */
    putBestScoreWithinInfinite(bestScore) {
        let store = this.getInstance();
        store.putSync(Config.KEY_BEST_SCORE_WITHIN_INFINITE, bestScore);
    },
/**
     * 获取无限模式下最高分
     * @return bestScore
     */
    getBestScoreWithinInfinite() {
        let store = this.getInstance();
        let bestScore = store.getSync(Config.KEY_BEST_SCORE_WITHIN_INFINITE, 0);
        return bestScore;
    },
/**
     * 保存当前无限模式分数
     * @param currentScore
     */
    putCurrentScoreWithinInfinite(currentScore) {
        let store = this.getInstance();
        store.putSync("KEY_CURRENT_SCORE_INFINITE", currentScore);
    },
/**
     * 获取当前无限模式分数
     * @return currentScore
     */
    getCurrentScoreWithinInfinite() {
        let store = this.getInstance();
        let currentScore = store.getSync("KEY_CURRENT_SCORE_INFINITE", 0);
        return currentScore;
    },
/**
     * 保存游戏难度
     * @param difficulty
     */
    putGameDifficulty(difficulty) {
        let store = this.getInstance();
        store.putSync(Config.SAVE_GAME_DIFFICULTY, difficulty);
    },
/**
     * 获取游戏难度
     * @return game_difficulty
     */
    getGameDifficulty() {
        let store = this.getInstance();
        let game_difficulty = store.getSync(Config.KEY_GAME_DIFFICULTY, 4);
        return game_difficulty;
    },
/**
     * 保存游戏音效状态
     * @param volumeState
     */
    putGameVolume(volumeState) {
        let store = this.getInstance();
        store.putSync(Config.KEY_GAME_VOLUME_STATE, volumeState);
    },
/**
     * 获取游戏音效状态
     * @return volumeState
     */
    getGameVolumeState() {
        let store = this.getInstance();
        let volumeState = store.getSync(Config.KEY_GAME_VOLUME_STATE, true);
        return volumeState;
    },
/**
     * 保存达成游戏目标次数
     * @param time
     */
    putGoalTime(time) {
        let store = this.getInstance();
        store.putSync(Config.KEY_GET_GOAL_TIME, time);
    },
/**
     * 获取达成游戏目标次数
     */
    getGoalTime() {
        let store = this.getInstance();
        let time = store.getSync(Config.KEY_GET_GOAL_TIME, 0);
        return time;
    },
/**
     * 保存游戏模式
     * @param mode
     */
    putCurrentGameMode(mode) {
        let store = this.getInstance();
        store.putSync(Config.KEY_CURRENT_GAME_MOE, mode);
    },
/**
     * 获取游戏模式
     */
    getCurrentGameMode() {
        let store = this.getInstance();
        let mode = store.getSync(Config.KEY_CURRENT_GAME_MOE, 0);
        return mode;
    }
}